<?php
/*
	代理商管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?:'add';
$tabs=array(
	array('op'=>'add','title'=>'添加代理'),
	
);
set_html('tabs',$tabs);
if($_RQ['op']=='add'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$mid=intval($post['mid']);
		if(!$mid){
			exi('请先选择会员','error');
		}
		$data=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$mid,
			'createtime'=>TIMESTAMP,
			'status'=>1,
		);
		if($post['pcd']){
			if($post['pcd']['province']){
				//省级代理
				$code=pdo_getcolumn('core_area',array('type'=>1,'title'=>$post['pcd']['province']),'code');
				$type=1;
			}
			if($post['pcd']['city']){
				$code=pdo_getcolumn('core_area',array('type'=>2,'parent_code'=>$code,'title'=>$post['pcd']['city']),'code');
				$type=2;
			}
			if($post['pcd']['district']){
				$code=pdo_getcolumn('core_area',array('type'=>3,'parent_code'=>$code,'title'=>$post['pcd']['district']),'code');
				$type=3;
			}
			$check=$this->getcolumn('agent',array('uniacid'=>$_SESSION['uniacid'],'area_code'=>$code),'id');
			if($check){
				exi('该地区已存在代理商，请选择其他地区！','error');
			}
			$data['type']=$type;
			$data['area_code']=$code;
		}
		$this->insert('agent',$data);
		exi('添加代理商成功！','','agent.agent_area/list');
	}
	
	
	$htmls=array(
		'type'=>'edit',
		'edit'=>array(
			array('type'=>'choose','name'=>'mid','title'=>'会员ID','plugin'=>'core','action'=>'member.get_member'),
			array('type'=>'radio','name'=>'type','title'=>'代理商类型','options'=>'types','show'=>array('agent_id'=>0)),
			array('type'=>'pcd','name'=>'pcd','title'=>'设置代理地区','options'=>'areas'),
		),
	);
	$return['areas']=cfc('tools')->pcd();
	$return['htmls']=$htmls;
	$item=array();
	
	
	if($_RQ['mid']){
		$item['mid']=intval($_RQ['mid']);
		
	}
	
	$return['item']=$item;
	$return['types']=$this->agent_types();
	$return['stops']=array(
		'0'=>'正常',
		'1'=>'禁用',
	);
	exi($return);
}